Add a CryptoProvider interface and NodeCryptoProvider implementation. #1237
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Notify
r? @richardm-stripe
Summary
Adds an
CryptoProvider
interface and moves out the Nodecrypto
logic into aNodeCryptoProvider
implementation.This initially only supports computing HMAC signatures, but lets us support this in a pluggable way. The Webhooks functions are updated to support passing in a custom provider. This can be used in environments where no 'crypto' package is available (removing another Node dependency for #997).
Test plan
Added a small common set of test cases which can be re-used by any implementation to ensure they meet the contract.
Looking forwards
Some crypto APIs (eg. the Web Crypto API) only provide async functions for computing HMACs (and other primitives). In the future, we may want to extend this interface with an
asyncComputeHMACSignature
function that returns a promise alongsidecomputeHMACSignature
. We would need to update the Webhooks code to support both sync and async computations (eg. providingasyncConstructEvent
andasyncVerifyHeader
).